--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]


local pmeta = FindMetaTable( "Player" )


function pmeta:ApplyPlayerSettings()

	self:Freeze( false )	
	self:SetJumpPower( 160 )	
	self:SetNWBool( "sitting", false )	
	self:SetNWBool( "ishigh", false )	
	self:SetNWInt( "stamina", 100 )	
	self:SetNWInt( "energy", 100 )
	self.hasFlashlight = false
	self.canRun = true
	self.arrested = false
	self.arrester = nil
	
	if self:GetNWBool( "sleeping" ) then
	
		self:SetNWBool( "sleeping", false )
			
		self:UnSpectate()

		if self.sp:IsValid() then

			self:SetPos( self.sp:GetPos() )
			self.sp:Remove()
			
		end
		
		if timer.IsTimer( self:UniqueID().."SleepTimer" ) then

			timer.Destroy( self:UniqueID().."SleepTimer" )

		end
		
		self:SetHealth( self.oldHealth )
		self:SetNWInt( "energy", self.oldEnergy )

	end	
	
	if ( !self:GetPData( "rpmodel" ) ) then --if player has joined for a first time and doesn't have RP model set
	
		self:SetModel( citizenmodels[ math.random( #citizenmodels ) ] )
		
	else	
	
		self:SetModel( self:GetPData( "rpmodel" ) )
		
	end			
	
	if self:IsOnFire() then
	
		self:Extinguish()
		
	end

	self:LoadOut()
	
	GAMEMODE:SetPlayerSpeed( self, 150, 300 )	
	
end	


function pmeta:SpawnProtection()

	self:GreenNotify( "Spawn protection activated." )
	self:GodEnable()
	self:SetColor( 255, 255, 255, 100 )

	timer.Simple( 5, function()
	
		if self:IsValid() && self:Alive() then
		
			self:RedNotify( "Spawn protection deactivated." )
			self:GodDisable()
			self:SetColor( 255, 255, 255, 255 )
			
		end	
		
	end )	
	
end	


function pmeta:LoadOut()

	self:StripWeapons()

	self:Give( "weapon_physcannon" ) 
	self:Give( "weapon_physgun" ) 
	self:Give( "gmod_camera" ) 
	self:Give( "gmod_rp_hands" )
	self:Give( "gmod_rp_keys" )
	self:Give( "gmod_tool" )
	
	for k, v in pairs( Jobs[self:getJob()][6] ) do
	
		self:Give( v )
		
	end	
	
end	


function pmeta:RealisticDamage()

	self:EmitSound( "vo/npc/male01/pain0"..math.random( 1, 9 )..".wav" )
	
end

function pmeta:applyPayDay()

	if timer.IsTimer( self:UniqueID().."PayDayTimer" ) then
	
		timer.Destroy( self:UniqueID().."PayDayTimer" )
	
	end
	
	timer.Create( self:UniqueID().."PayDayTimer", tonumber( Configuration["paydayint"] ), 0, function()
		
		self:GreenNotify( "Pay day!" )
		self:AddMoney( Jobs[self:getJob()][4] )
			
	end )	
	
end	


function pmeta:SetPlayerEnergy( amount )

	self:SetNWInt("energy", tonumber(amount))
	
end	


function pmeta:RPTalk( range, txt )

	for k, v in pairs( ents.FindInSphere( self:GetPos(), range ) ) do
	
		if v:IsPlayer() && v:Alive() then
		
			v:ChatPrint( self:Nick()..": "..tostring( txt ) )
			
		end
		
	end

end	


GMRP.command( "sit", function( p )

	if GMRP.UseLimit( p ) then

		if p:GetNWBool( "sleeping" ) then

			p:RedNotify( "You can not sit while sleeping." )
			return
			
		end	

		if p:GetNWBool( "sitting" ) then
		
			p:SetNWBool( "sitting", false )
			p:SetMoveType( MOVETYPE_WALK )
			p:GreenNotify( "You are standing." )
			
		else
		
			p:SetNWBool( "sitting", true )
			p:SetMoveType( MOVETYPE_NONE )
			p:GreenNotify( "You are sitting." )
			
		end
		
	end	
	
end )


GMRP.command( "sleep", function( p )

	if GMRP.UseLimit( p ) then

		if p:GetNWBool( "sitting" ) then

			p:RedNotify( "You can not sleep while sitting." )
			return
			
		end	
		
		
		if p:Alive() then
		
			if p:GetNWBool( "sleeping" ) then
			
				p:Spawn()
				p:GreenNotify( "You woke up." )
				
			else
			
			
				if ( !p:IsOutside() ) then
				
					p:SetNWBool( "sleeping", true )
					p:Freeze( true )

					p.sp = ents.Create( "prop_ragdoll" )
					p.sp:SetPos( p:GetPos() )
					p.sp:SetAngles( Angle( 0, p:GetAngles().Yaw, 0 ) )
					p.sp:SetModel(p:GetModel())
					p.sp:Spawn()
					p.sp:Activate()
					p.sp:SetVelocity( p:GetVelocity() )
					p.oldHealth = p:Health()
					p.oldEnergy = p:GetNWInt( "energy" )				
					p:StripWeapons()
					p:Spectate( OBS_MODE_CHASE )
					p:SpectateEntity( p.sp )			
					
					timer.Create( p:UniqueID().."SleepTimer", 5, 0, function()
					
						if p.oldHealth < 100 then
					
							p.oldHealth = p.oldHealth + 1
					
						end
						
						if p.oldEnergy > 0 then
						
							p.oldEnergy = p.oldEnergy - 5
							
						end	
						
						if !p:Alive() then
						
							p:Freeze( false )
							p:SetNWBool( "sleeping", false )		
							timer.Destroy( p:UniqueID().."SleepTimer" )	
							
						end	
						
					end )
					
					p:GreenNotify( "You are sleeping." )
					
				else
				
					p:RedNotify( "You can not sleep outside." )
					
				end	
				
			end
		
		end
		
	end	
	
end )


function GM:SetPlayerAnimation( p, weapanim )

	self.BaseClass:SetPlayerAnimation( p, weapanim )
	
	if p:GetNWBool( "sitting" ) then
	
		local seq = p:LookupSequence( "sit" )	
		p:SetPlaybackRate( 1 )	
		p:ResetSequence( seq )
		p:SetCycle( 1 )
		
	end

end	


local unDropable = { "hand", "keys" }

GMRP.command( "dropweapon", function( p )
	
	if GMRP.UseLimit( p ) then
	
		local actWeap = p:GetActiveWeapon()

		if actWeap:IsValid() then
		
			for _, v in pairs( unDropable ) do
		
				if !string.find( actWeap:GetClass(), v ) then
				
					p:DropWeapon( actWeap )
					
				end

			end
			
		end
		
	end	

end	)


function pmeta:CleanStuff()

	self:cleanEntOwnerShip()
	
	if timer.IsTimer( self:UniqueID().."PayDayTimer" ) then
	
		timer.Destroy( self:UniqueID().."PayDayTimer" )
		
	end	
	
	if timer.IsTimer( self:UniqueID().."SleepTimer" ) then
	
		timer.Destroy( self:UniqueID().."SleepTimer" )
		
	end	
	
end	